const App = getApp();
Page({
  data: {
    isLogin: false,
    teamId:0,
    category_id:0,
    detail:{article_status:1},
    maoHao: '：',
    // 编辑器editor
    formats: {},
    readOnly: false,
    placeholder: '请输入关于我们内容...',
    editorHeight: 0,
    keyboardHeight: 0,
    isIOS: false,
    editorContent:''//提交的内容
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      category_id:options.nid,
    })
    this.updatePosition(246);
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let that = this;
    that.setData({
      isLogin: App.checkIsLogin(),
    });
    if (that.data.isLogin) {
      if(that.data.category_id > 0){
        // 详情
        that.getDetail();
      }
    }
  },
  
  /**
   * 详情
   */
  getDetail() {
    let _this = this;
    let obj = {cate_id:this.data.category_id}
    App._get('article/aboutus', obj, result => {
      _this.setData({
        detail: result.data.detail,
        placeholder: '如果内容没有显示，请后退重新进入！',
        editorContent:result.data.detail.introduction_content //编辑器回显内容赋值
      })
      _this.setEditorContent();//编辑器回显
    });
  },

  //提交
  formSubmit(e) {
    var formData=e.detail.value;//获取表单数据
    if(formData.cate_name == ""){
      App.showError("导航标题不能空!");
      return;
    }
    if(formData.sort == ""){
      App.showError("排序不能空!");
      return;
    }
    let obj = {
      category_id:this.data.category_id,
      name:formData.cate_name,
      sort:formData.sort,
      introduction_content:this.data.editorContent, //内容
    };
    App._post_form('article/aboutus_edit', obj, result => {
      //成功提示
      App.showSuccess(result.msg, function() {
        wx.navigateTo({
          url: "../index",
        })
      });
    });
  },
/*
  *---------------------------------------------------编辑器--------------------------------------
  */
  // submit的时候 获取editor里的内容
  getContent() {
    let that = this;
    this.editorCtx.getContents({
        success: (res) => {
            that.setData({
              editorContent: res.html
            })
        }
    })
  },
  // 编辑内容回显
  setEditorContent(){
    const that = this
    wx.createSelectorQuery().select('#editor').context(function (res) {
      that.editorCtx.setContents({
          html: that.data.editorContent //回显内容
        })
    }).exec();
    this.getContent;
  },
  updatePosition(keyboardHeight) {
    const toolbarHeight = 0
    const { windowHeight, platform } = wx.getSystemInfoSync()
    let editorHeight = keyboardHeight > 0 ? (windowHeight - keyboardHeight - toolbarHeight) : windowHeight
    console.log(editorHeight);
    this.setData({ editorHeight, keyboardHeight })
  },
  calNavigationBarAndStatusBar() {
    const systemInfo = wx.getSystemInfoSync()
    const { statusBarHeight, platform } = systemInfo
    const isIOS = platform === 'ios'
    const navigationBarHeight = isIOS ? 44 : 48
    return statusBarHeight + navigationBarHeight
  },
// 注册编辑id = editorCtx
  onEditorReady() {
    const that = this
    wx.createSelectorQuery().select('#editor').context(function (res) {
      that.editorCtx = res.context
    }).exec()
  },
  blur() {
    console.log('blur')
    this.editorCtx.blur()
  },
  format(e) {
    let { name, value } = e.target.dataset
    if (!name) return
     console.log('format', name, value)
    this.editorCtx.format(name, value)

  },
  onStatusChange(e) {
    const formats = e.detail
    console.log(formats);
    this.setData({ formats })
  },
  insertDivider() {
    this.editorCtx.insertDivider({
      success: function () {
        console.log('insert divider success')
      }
    })
  },
  clear() {
    this.editorCtx.clear({
      success: function (res) {
        console.log("clear success")
      }
    })
  },
  removeFormat() {
    this.editorCtx.removeFormat()
  },
  insertDate() {
    const date = new Date()
    const formatDate = `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`
    this.editorCtx.insertText({
      text: formatDate
    })
  },
  insertImage() {
    const that = this
    wx.chooseImage({
      count: 1,
      success: function (respone) {
        let file = respone.tempFiles[0];
        console.log(file.size)
        // 不能大于1m
        if(file.size>1024*1024){
          App.showError("图片不能超过1M！");
          return;
        }
        
      //  上传图
          App.uploadFile(file.path,{},function(result){
              console.log(result.data.file_path)
              // 插入编辑器
              that.editorCtx.insertImage({
                src: result.data.file_path,
                data: {
                  id: 'abcd',
                  role: 'god'
                },
                width: '80%',
                success: function () {
                  console.log('insert image success')
                }
              })
          })

      }
    })
  }
})